package mt.omid.dimomuth.internal;


import static mt.omid.dimomuth.internal.ErrorList.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;


public class ConfigManager
{

    Properties props    = null;
    String     confAddr = null;


    public ConfigManager( String confFile ) throws ConfigurationException
    {
	if( confFile == null )
	{
	    props = new Properties();
	    return;
	}
	this.confAddr = confFile;
	props = new Properties();
	try
	{
	    props.load( new FileInputStream( new File( this.confAddr ) ) );
	}
	catch( FileNotFoundException e )
	{
	    throw new ConfigurationException( CFG_FILE_NOT_FND + ":Error in finding config file["
		    + this.confAddr + "]", e );
	}
	catch( IOException e )
	{
	    throw new ConfigurationException( CFG_ERR_READ_FILE + ":Error in reading config file["
		    + this.confAddr + "]", e );
	}
    }

    public String getProperty( String key )
    {
	if( props == null )
	    return null;
	return props.getProperty( key );
    }

    public String getProperty( String key, String defaultVal )
    {
	if( props == null )
	    return defaultVal;
	return props.getProperty( key, defaultVal );
    }

    public int getIntProperty( String key )
    {
	if( props == null )
	    return 0;
	try
	{
	    return Integer.parseInt( props.getProperty( key ) );
	}
	catch( NumberFormatException nfex )
	{
	    System.err.println( "Bad config value for key " + key );
	    return 0;
	}

    }

    public int getIntProperty( String key, int defaultVal )
    {
	if( props == null )
	    return defaultVal;
	try
	{
	    return Integer.parseInt( props.getProperty( key ) );
	}
	catch( NumberFormatException nfex )
	{
	    System.err.println( "Bad config value for key " + key );
	    return defaultVal;
	}
    }

    
    public final static String DEFAULT_KRN_CFG_FILE = "krn.cfg";
}
